<?php
/**
 * Created by PhpStorm.
 * User: yh
 * Date: 2017/11/23
 * Time: 下午1:28
 */

namespace app\user\model;
include_once EXTEND_PATH . "alipay/AopSdk.php";

class Zfb
{
    static public function aliResponse($body, $orderSn, $price, $notify = '')
    {
        require_once EXTEND_PATH . "alipay/aop/request/AlipayTradeWapPayRequest.php";
        $request = new \AlipayTradeWapPayRequest();
        $c = self::getAop();
        $request->setNotifyUrl($notify);//回调地址
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $biz = [
            "subject"=>$body,
            "out_trade_no"=>$orderSn,
            "total_amount"=>$price,
            "product_code"=>"QUICK_WAP_PAY",
            "quit_url"=>url('index/index', [],'html', true)
        ];
        $request->setBizContent(json_encode($biz, JSON_UNESCAPED_UNICODE));
        $response = $c->pageExecute($request, "get");//这里就是sdk自己的方法
        return $response;
    }

    /**
     * 查询订单状态
     * @param $transaction_id
     * @return string
     */
    public function getPayStatus($transaction_id)
    {
// 顶部已引入           require_once EXTEND_PATH."alipay/AopSdk.php";
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        require_once EXTEND_PATH . "alipay/aop/request/AlipayTradeQueryRequest.php";
        $aop = new \AopClient ();
        $aop->gatewayUrl = config('alipay.gatewayUrl');
        $aop->appId = config('alipay.app_id');
        $aop->rsaPrivateKey = config('alipay.rsaPrivateKey');
        $aop->alipayrsaPublicKey = config('alipay.alipayrsaPublicKey');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayTradeQueryRequest ();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"$transaction_id\"}");
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            $status = 1;
        } else {
            $status = 0;
        }
        return $status;
    }

    static public function getAop()
    {
        require_once EXTEND_PATH . "alipay/aop/AopCertClient.php";
        $aop = new \AopCertClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config("app.alipay.app_id");
        $aop->rsaPrivateKey = config('app.alipay.rsaPrivateKey');
        $aop->alipayrsaPublicKey = $aop->getPublicKey(config('app.alipay.alipay_cert_public_path'));//调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';
        $aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN(config('app.alipay.app_cert_path'));//调用getCertSN获取证书序列号
//        $aop->alipayRootCertSN = '687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6';//调用getRootCertSN获取支付宝根证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN(config('app.alipay.root_cert_path'));
        return $aop;
    }
}